﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using CapstoneProjectAEReader.Entities.Models;
using CapstoneProjectAEReader.DataAccess.DBAccess;
using CapstoneProjectAEReader.Common.Utility;
using CapstoneProjectAEReader.GUIs.ReaderGUI;

namespace CapstoneProjectAEReader.GUIs.Others
{
    public partial class Exist : Window
    {
        #region Attributes

        private const string BtSaveName = "saveButton";
        private const string BtCancelName = "cancelButton";
        private Note _note;
        private DAO _dao;
        private int _bookId;
        private bool _status = false;
        private NoteDetail _noteDetail;
        private ReaderGui _parentGui;

        private const string Ebook = "ebook";
        private const string PageNo = "page";
        private const string Date = "date";
        private const string Description = "description";
        private const string NoteDetail = "noteDetail";
        private const string DisplayEbook = "displayEbook";
        private const string DisplayNoteDetail = "displayNoteDetail";
        private const string Note = "note";

        #endregion

        #region Methods

        public Exist(Note note, DTO dto, NoteDetail noteDetail, ReaderGui parent)
        {
            _parentGui = parent;
            _note = note;
            _dao = new DAO(dto);
            _bookId = noteDetail.Bookid;
            _noteDetail = noteDetail;
            InitializeComponent();
            ReloadFormLanguage();
            BindColumnName(0);
            noteListBox.ItemsSource = GenerateItemSource(_bookId, 0);
            if (_dao.CheckNoteDetailExist(_note.NoteId, _bookId))
            {
                BindNoteDetail(_dao.GetNoteDetail(_note.NoteId, _bookId));
                _status = true;
            }
            else
            {
                BindNoteDetail(_noteDetail);
            }
        }

        public void ReloadFormLanguage()
        {
            btCancelName.Content = ResourceReader.GetResourceContent(BtCancelName);
            btSaveName.Content = ResourceReader.GetResourceContent(BtSaveName);
            lbNoteWord.Content = _note.NoteContent;
            lbBookTitle.Content = ResourceReader.GetResourceContent(Ebook);
            lbDate.Content = ResourceReader.GetResourceContent(Date);
            lbPageNo.Content = ResourceReader.GetResourceContent(PageNo);
            lbDesCription.Content = ResourceReader.GetResourceContent(Description);
            lbNoteDetail.Content = ResourceReader.GetResourceContent(NoteDetail);
            rbAllBook.Content = ResourceReader.GetResourceContent(DisplayEbook);
            rbAllDetail.Content = ResourceReader.GetResourceContent(DisplayNoteDetail);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="mode">0: book, 1: note</param>
        public void BindColumnName(int mode)
        {
            if (mode == 0)
            {
                firstColumn.Header = ResourceReader.GetResourceContent(Note);
            }
            else
            {
                firstColumn.Header = ResourceReader.GetResourceContent(Ebook);
            }
            secondColumn.Header = ResourceReader.GetResourceContent(PageNo);
            thirdColumn.Header = ResourceReader.GetResourceContent(Description);
        }

        /// <summary>
        /// Generate Item Source Listview
        /// </summary>
        /// <param name="id"></param>
        /// <param name="mode">0: book, 1: Note</param>
        /// <returns></returns>
        private List<GridContent> GenerateItemSource(int id, int mode)
        {
            List<GridContent> rtList = new List<GridContent>();
            if (mode == 0)
            {
                List<NoteDetail> notesOfBook = _dao.GetNoteDetailWithBookId(id);
                foreach (var noteDetail in notesOfBook)
                {
                    Note parentNote = _dao.GetNoteById(noteDetail.Noteid);
                    GridContent temp = new GridContent();
                    temp.NoteData = parentNote;
                    temp.NoteDetailData = noteDetail;
                    temp.FirstColumn = parentNote.NoteContent;
                    temp.SecondColumn = noteDetail.PageNumber.ToString();
                    temp.ThirdColumn = noteDetail.Description;
                    rtList.Add(temp);
                }
            }
            if (mode == 1)
            {
                List<NoteDetail> notesOfBook = _dao.GetNoteDetailWithId(id);
                foreach (var noteDetail in notesOfBook)
                {
                    Note parentNote = _dao.GetNoteById(id);
                    GridContent temp = new GridContent();
                    temp.NoteData = parentNote;
                    temp.NoteDetailData = noteDetail;
                    temp.FirstColumn = _dao.GetEbookById(noteDetail.Bookid).Title;
                    temp.SecondColumn = noteDetail.PageNumber.ToString();
                    temp.ThirdColumn = noteDetail.Description;
                    rtList.Add(temp);
                }
            }
            return rtList;
        }

        private void BindNoteDetail(NoteDetail detail)
        {
            lbBookTitleDt.Content = _dao.GetEbookById(detail.Bookid).Title;
            lbDateDt.Content = detail.Createdate;
            lbPageNoDt.Content = detail.PageNumber;
            FlowDocument tempDoc = new FlowDocument();
            tempDoc.Blocks.Add(new Paragraph(new Run(detail.Description)));
            txtDescription.Document = tempDoc;
        }

        private void noteListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (noteListBox.SelectedItem != null)
            {
                GridContent selectedItem = (GridContent) noteListBox.SelectedItem;
                _status = true;
                BindNoteDetail(selectedItem.NoteDetailData);
                lbNoteWord.Content = selectedItem.NoteData.NoteContent;
                if (selectedItem.NoteDetailData.Noteid != _noteDetail.Noteid &&
                    selectedItem.NoteDetailData.Bookid != _noteDetail.Bookid)
                {
                    txtDescription.IsReadOnly = true;
                }
                else
                {
                    txtDescription.IsReadOnly = false;
                }
            }
        }

        private void HandleRbAllBook(object sender, RoutedEventArgs e)
        {
            rbAllDetail.IsChecked = false;
            rbAllBook.IsChecked = true;
            BindColumnName(0);
            noteListBox.ItemsSource = GenerateItemSource(_bookId, 0);
        }

        private void HandleRbAllDetail(object sender, RoutedEventArgs e)
        {
            rbAllDetail.IsChecked = true;
            rbAllBook.IsChecked = false;
            BindColumnName(1);
            noteListBox.ItemsSource = GenerateItemSource(_note.NoteId, 1);
            lbNoteWord.Content = _note.NoteContent;
        }

        private void SaveButtonHandler(object sender, RoutedEventArgs e)
        {
            NoteDetail temp = new NoteDetail();
            if (_status)
            {
                temp = _dao.GetNoteDetail(_note.NoteId, _bookId);
                var textRange = new TextRange(txtDescription.Document.ContentStart, txtDescription.Document.ContentEnd);
                temp.Description = textRange.Text;
                _dao.UpdateNoteDetail(temp);
            }
            else
            {
                temp.Noteid = _note.NoteId;
                temp.Bookid = _bookId;
                temp.Createdate = DateTime.Now.ToString(Constants.FullDateTimeFormatToSecond);
                var textRange = new TextRange(txtDescription.Document.ContentStart, txtDescription.Document.ContentEnd);
                temp.Description = textRange.Text;
                _dao.InsertNoteDetail(temp);
            }
            Close();
        }

        private void CancelButtonHandler(object sender, RoutedEventArgs e)
        {
            Close();
        }

        #endregion

        private void Window_Closed(object sender, EventArgs e)
        {
            _parentGui.RebindData();
        }
    }

    public class GridContent
    {
        public Note NoteData = new Note();
        public NoteDetail NoteDetailData = new NoteDetail();
        private string _firstColumn;
        private string _secondColumn;
        private string _thirdColumn;

        public string FirstColumn
        {
            get { return _firstColumn; }
            set { _firstColumn = value; }
        }

        public string SecondColumn
        {
            get { return _secondColumn; }
            set { _secondColumn = value; }
        }

        public string ThirdColumn
        {
            get { return _thirdColumn; }
            set { _thirdColumn = value; }
        }
    }
}